<template>
  <a-modal title="轮播图管理" :width="640" :visible="visible" :confirmLoading="confirmLoading" :maskClosable="false" @ok="handleSubmit"
           @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <div class="step-form-style-desc">
        <h4>每日签到：/pages/home/qiandao/index</h4>
        <h4>礼品商城：/pages/home/integral/index</h4>
        <h4>幸运转盘：/pages/home/canvas/index</h4>
        <h4>品牌中心：/pages/home/storeList/index</h4>
        <h4>直播中心：/pages/home/broadcast/index</h4>
        <h4>预约商品：/pages/home/couponList/index</h4>
        <h4>快速积分：ksjf</h4>
      </div>
      <a-divider />
      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="小程序">
          <a-select showSearch optionFilterProp="children" :filterOption="filterOption" :disabled="isDisabled"
                    v-decorator="['customerId', {rules: [{required: true, message: '必输'}]}]">
            <a-select-option v-for="item in editProps" :key="item.id">{{item.name}}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="名称">
          <a-input v-decorator="[ 'name', {rules: [{ required: true, message: '请输入。。。' }]}]"></a-input>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="跳转类型">
          <a-select v-decorator="[ 'transUrlType', {rules: []} ]" >
            <a-select-option :value="1">内部跳转</a-select-option>
            <a-select-option :value="2">网页跳转</a-select-option>
            <a-select-option :value="3">小程序跳转</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="跳转地址">
          <a-input v-decorator="[ 'transUrl', {rules: [] }]"/>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="排序">
          <a-input v-decorator="[ 'sortNo', {rules: [] }]"/>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="图片" extra="轮播图推荐500*250，九宫格图推荐73*73，背景图推荐690*340">
          <a-upload
            listType="picture-card"
            class="avatar-uploader"
            :showUploadList="false"
            :action="baseUrl + '/system/common/uploadFile'"
            :beforeUpload="beforeUpload"
            @change="uploadChange"
          >
            <img v-if="imageUrl" :src="imageUrl" alt="avatar" width="300px"/>
            <div v-else>
              <a-icon :type="confirmLoading ? 'loading' : 'plus'" />
              <div class="ant-upload-text">Upload</div>
            </div>
          </a-upload>
        </a-form-item>
      </a-form>

    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import { saveOrUpdate } from '@/api/mini/pic'
  /**
   * 数据
   */
  const data = function() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 }
      },
      id: null,
      visible: false,
      confirmLoading: false,
      isDisabled: false,
      //图片相关
      baseUrl: process.env.VUE_APP_API_BASE_URL,
      imageUrl: '',
      fileName: '',
      form: this.$form.createForm(this),
    }
  }

  const created = function() {

  }

  export default {
    props: {
      editProps: {
        type: Array,
        default: null
      }
    },
    data: data,
    created: created,
    /**
     * 方法
     */
    methods: {
      add () {
        this.visible = true
      },
      edit (record) {
        this.visible = true
        this.id = record.id
        this.isDisabled = true
        this.imageUrl = record.fileUrl
        const { form: { setFieldsValue } } = this
        this.$nextTick(() => {
          const formData = pick(record, ['customerId', 'name', 'transUrlType', 'transUrl', 'sortNo'])
          setFieldsValue(formData)
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            values.id = this.id
            values.type = 1
            values.fileUrl = this.imageUrl
            saveOrUpdate(values).then((res) => {
              this.confirmLoading = false
              if (res.code === 200) {
                this.handleCancel()
                this.$emit('ok')
              }
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.id = null
        this.visible = false
        this.isDisabled = false
        this.confirmLoading = false
        this.form.resetFields()
        this.imageUrl = null
        this.filename = null
      },
      filterOption (input, option) {
        return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      },
      uploadChange(info) {
        if (info.file.status === 'uploading') {
          this.confirmLoading = true;
          return;
        }
        if (info.file.status === 'done') {
          // Get this url from response in real world.
          this.imageUrl = info.file.response.fileUrl;
          this.filename = info.file.response.filename
          this.confirmLoading = false;
        }
      },
      beforeUpload(file) {
        return true;
      },
    },
  }
</script>

<style>
  .step-form-style-desc {
    padding: 0 56px;
    color: rgba(0, 0, 0, .45);
  }
  h3 {
    margin: 0 0 12px;
    color: rgba(0,0,0,.45);
    font-size: 16px;
    line-height: 32px;
  }

  h4 {
    margin: 0 0 4px;
    color: rgba(0,0,0,.45);
    font-size: 14px;
    line-height: 22px;
  }

  p {
    margin-top: 0;
    margin-bottom: 12px;
    line-height: 22px;
  }
</style>